home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Lib / test / regrtest.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-23  |  9.5 KB  |  309 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 1.5)
  3.  
  4. '''Regression test.
  5.  
  6. This will find all modules whose name is "test_*" in the test
  7. directory, and run them.  Various command line options provide
  8. additional facilities.
  9.  
  10. Command line options:
  11.  
  12. -v: verbose  -- run tests in verbose mode with output to stdout
  13. -q: quiet    -- don\'t print anything except if a test fails
  14. -g: generate -- write the output file for a test instead of comparing it
  15. -x: exclude  -- arguments are tests to *exclude*
  16. -s: single   -- run only a single test (see below)
  17.  
  18. If non-option arguments are present, they are names for tests to run,
  19. unless -x is given, in which case they are names for tests not to run.
  20. If no test names are given, all tests are run.
  21.  
  22. -v is incompatible with -g and does not compare test output files.
  23.  
  24. -s means to run only a single test and exit.  This is useful when Purifying
  25. the Python interpreter.  The file /tmp/pynexttest is read to find the next
  26. test to run.  If this file is missing, the first test_*.py file in testdir or
  27. on the command line is used.  (actually tempfile.gettempdir() is used instead
  28. of /tmp).
  29.  
  30. '''
  31. import sys
  32. import string
  33. import os
  34. import getopt
  35. import traceback
  36. import test_support
  37.  
  38. def main(tests = None, testdir = None):
  39.     """Execute a test suite.
  40.  
  41.     This also parses command-line options and modifies its behaviour
  42.     accordingly. 
  43.  
  44.     tests -- a list of strings containing test names (optional)
  45.     testdir -- the directory in which to look for tests (optional)
  46.  
  47.     Users other than the Python test suite will certainly want to
  48.     specify testdir; if it's omitted, the directory containing the
  49.     Python test suite is searched for.  
  50.  
  51.     If the tests argument is omitted, the tests listed on the
  52.     command-line will be used.  If that's empty, too, then all *.py
  53.     files beginning with test_ will be used.
  54.     
  55.     """
  56.     
  57.     try:
  58.         (opts, args) = getopt.getopt(sys.argv[1:], 'vgqxs')
  59.     except getopt.error:
  60.         msg = None
  61.         print msg
  62.         print __doc__
  63.         return 2
  64.  
  65.     verbose = 0
  66.     quiet = 0
  67.     generate = 0
  68.     exclude = 0
  69.     single = 0
  70.     for o, a in opts:
  71.         if o == '-q':
  72.             quiet = 1
  73.             verbose = 0
  74.         
  75.         if o == '-g':
  76.             generate = 1
  77.         
  78.         if o == '-x':
  79.             exclude = 1
  80.         
  81.         if o == '-s':
  82.             single = 1
  83.         
  84.     
  85.     if generate and verbose:
  86.         print "-g and -v don't go together!"
  87.         return 2
  88.     
  89.     good = []
  90.     bad = []
  91.     skipped = []
  92.     if single:
  93.         gettempdir
  94.         filename = os.path.join(gettempdir(), 'pynexttest')
  95.         
  96.         try:
  97.             fp = open(filename, 'r')
  98.             next = string.strip(fp.read())
  99.             tests = [
  100.                 next]
  101.             fp.close()
  102.         except IOError:
  103.             import tempfile
  104.             import tempfile
  105.         except:
  106.             import tempfile
  107.  
  108.     
  109.     for i in range(len(args)):
  110.         pass
  111.     
  112.     stdtests = STDTESTS[:]
  113.     nottests = NOTTESTS[:]
  114.     if exclude:
  115.         for arg in args:
  116.             pass
  117.         
  118.         nottests[:0] = args
  119.         args = []
  120.     
  121.     if not tests and args:
  122.         pass
  123.     tests = findtests(testdir, stdtests, nottests)
  124.     if single:
  125.         tests = tests[:1]
  126.     
  127.     test_support.verbose = verbose
  128.     for test in tests:
  129.         if not quiet:
  130.             print test
  131.         
  132.         ok = runtest(test, generate, verbose, testdir)
  133.         if ok > 0:
  134.             good.append(test)
  135.         elif ok == 0:
  136.             bad.append(test)
  137.         elif not quiet:
  138.             print 'test', test, 'skipped -- an optional feature could not be imported'
  139.         
  140.         skipped.append(test)
  141.     
  142.     if good and not quiet:
  143.         if not bad and not skipped and len(good) > 1:
  144.             print 'All',
  145.         
  146.         print count(len(good), 'test'), 'OK.'
  147.     
  148.     if bad:
  149.         print count(len(bad), 'test'), 'failed:', string.join(bad)
  150.     
  151.     if skipped and not quiet:
  152.         print count(len(skipped), 'test'), 'skipped:', string.join(skipped)
  153.     
  154.     if single:
  155.         alltests = findtests(testdir, stdtests, nottests)
  156.         for i in range(len(alltests)):
  157.             if tests[0] == alltests[i]:
  158.                 break
  159.             
  160.         
  161.     
  162.     return len(bad) > 0
  163.  
  164. STDTESTS = [
  165.     'test_grammar',
  166.     'test_opcodes',
  167.     'test_operations',
  168.     'test_builtin',
  169.     'test_exceptions',
  170.     'test_types']
  171. NOTTESTS = [
  172.     'test_support',
  173.     'test_b1',
  174.     'test_b2']
  175.  
  176. def findtests(testdir = None, stdtests = STDTESTS, nottests = NOTTESTS):
  177.     '''Return a list of all applicable test modules.'''
  178.     if not testdir:
  179.         testdir = findtestdir()
  180.     
  181.     names = os.listdir(testdir)
  182.     tests = []
  183.     for name in names:
  184.         if name[:5] == 'test_' and name[-3:] == '.py':
  185.             modname = name[:-3]
  186.             if modname not in stdtests and modname not in nottests:
  187.                 tests.append(modname)
  188.             
  189.         
  190.     
  191.     tests.sort()
  192.     return stdtests + tests
  193.  
  194.  
  195. def runtest(test, generate, verbose, testdir = None):
  196.     '''Run a single test.
  197.     test -- the name of the test
  198.     generate -- if true, generate output, instead of running the test
  199.     and comparing it to a previously created output file
  200.     verbose -- if true, print more messages
  201.     testdir -- test directory
  202.     '''
  203.     test_support.unload(test)
  204.     if not testdir:
  205.         testdir = findtestdir()
  206.     
  207.     outputdir = os.path.join(testdir, 'output')
  208.     outputfile = os.path.join(outputdir, test)
  209.     
  210.     try:
  211.         if generate:
  212.             cfp = open(outputfile, 'w')
  213.         elif verbose:
  214.             cfp = sys.stdout
  215.         else:
  216.             cfp = Compare(outputfile)
  217.     except IOError:
  218.         cfp = None
  219.         print "Warning: can't open", outputfile
  220.  
  221.     
  222.     try:
  223.         save_stdout = sys.stdout
  224.         
  225.         try:
  226.             if cfp:
  227.                 sys.stdout = cfp
  228.                 print test
  229.             
  230.             __import__(test, globals(), locals(), [])
  231.         finally:
  232.             sys.stdout = save_stdout
  233.  
  234.     except ImportError:
  235.         msg = None
  236.         return -1
  237.     except KeyboardInterrupt:
  238.         v = None
  239.         raise KeyboardInterrupt, v, sys.exc_info()[2]
  240.     except test_support.TestFailed:
  241.         msg = None
  242.         print 'test', test, 'failed --', msg
  243.         return 0
  244.     except:
  245.         (type, value) = sys.exc_info()[:2]
  246.         print 'test', test, 'crashed --', type, ':', value
  247.         if verbose:
  248.             traceback.print_exc(file = sys.stdout)
  249.         
  250.         return 0
  251.  
  252.     return 1
  253.  
  254.  
  255. def findtestdir():
  256.     if __name__ == '__main__':
  257.         file = sys.argv[0]
  258.     else:
  259.         file = __file__
  260.     if not os.path.dirname(file):
  261.         pass
  262.     testdir = os.curdir
  263.     return testdir
  264.  
  265.  
  266. def count(n, word):
  267.     if n == 1:
  268.         return '%d %s' % (n, word)
  269.     else:
  270.         return '%d %ss' % (n, word)
  271.  
  272.  
  273. class Compare:
  274.     
  275.     def __init__(self, filename):
  276.         self.fp = open(filename, 'r')
  277.  
  278.     
  279.     def write(self, data):
  280.         expected = self.fp.read(len(data))
  281.         if data != expected:
  282.             raise test_support.TestFailed, 'Writing: ' + `data` + ', expected: ' + `expected`
  283.         
  284.  
  285.     
  286.     def writelines(self, listoflines):
  287.         map(self.write, listoflines)
  288.  
  289.     
  290.     def flush(self):
  291.         pass
  292.  
  293.     
  294.     def close(self):
  295.         leftover = self.fp.read()
  296.         if leftover:
  297.             raise test_support.TestFailed, 'Unread: ' + `leftover`
  298.         
  299.         self.fp.close()
  300.  
  301.     
  302.     def isatty(self):
  303.         return 0
  304.  
  305.  
  306. if __name__ == '__main__':
  307.     sys.exit(main())
  308.  
  309.